package com.vectz.mgt.action;

import java.util.ArrayList;

import com.vectz.mgt.model.Entity;
import com.vectz.mgt.model.EntityOverview;
import com.vectz.mgt.util.Constant;
import com.vectz.mgt.util.Log;
import com.vectz.mgt.util.Util;

/**
 * @author ai-t.vn
 * 
 */
public class IActionCodegenAction {
	ArrayList<Entity> listField=null;
	EntityOverview table = null;
	String outputFolder = "";
	public IActionCodegenAction(ArrayList<Entity> listField, EntityOverview table, String outputFolder)
				throws Exception {
		this.listField = listField;
		this.table = table;
		this.outputFolder = outputFolder;
		genernateIActionFile();
	}

	/*
	 * Generate java interface action file
	 */
	public void genernateIActionFile()  throws Exception{
			String tmplFormContent = Util.readTextFileContent(Util
					.getTmplPath("action", "IAction-File.txt"));

			
			tmplFormContent = tmplFormContent.replaceAll("xxxxPackageName",table.getPackageName());
			tmplFormContent = tmplFormContent.replaceAll("xxxxEntity", table.getName());
			tmplFormContent = tmplFormContent.replaceAll("xxxxLEntity", Util.lowerCaseFirstChar(table.getName()));
			tmplFormContent = tmplFormContent.replaceAll("xxxxPkType", Util.getPKType(listField));
			/* If form has fileUpload component
			 * Modify at 2008-10-25 by anhdv
			 */
			if(!Util.isEmpty(generateFileUploadMethod(tmplFormContent)))
				tmplFormContent = tmplFormContent.replaceAll("xxxxFileUploadMethod",generateFileUploadMethod(tmplFormContent));
			else
				tmplFormContent = tmplFormContent.replaceAll("xxxxFileUploadMethod","");
			
			String fileLocation = new StringBuffer().append(outputFolder)
			.append(System.getProperty("file.separator"))
			.append("src")
			.append(System.getProperty("file.separator"))
			.append("action")
			.append(System.getProperty("file.separator"))
			.append("local")
			.append(System.getProperty("file.separator"))
			.append("I")
			.append(table.getName())
			.append("Action.java").toString();
			
			Util.writeJsfFile(fileLocation,tmplFormContent);
			Log.log("Finish generate java file to:"+fileLocation);
	}
	/**
	 * generate fileUpload method 
	 * 
	 * @param tmplFormContent
	 * @return
	 */
	public String generateFileUploadMethod(String tmplFormContent){
		int numFileUpload = 0;
		String fileUploadMethod = "";
		StringBuffer tmp = new StringBuffer();
		if (listField != null) {
			Entity field = null;
			
			for (int i = 0; i < listField.size(); i++) {
				field = (Entity) listField.get(i);
				if(field.getInputDataType()!=null){
					if (Util.dbTypeToJavaType(field.getInputDataType()).equalsIgnoreCase("byte[]")){
						numFileUpload++;
					}
				}
			}
		}
		if(numFileUpload!= 0){
			for (int i = 1; i <= numFileUpload; i++) {
				//file upload
				tmp = tmp.append("public String getFileName"+i+"();\n")
					.append("public void setFileName"+i +"(String fileName);\n\n");
			}
			//index of file download
			tmp.append("public String getIndex();\n")
				.append("public void setIndex(String index);\n\n");
			
			fileUploadMethod = tmp.toString();
		}
		return fileUploadMethod;
	}
}
